package com.unnamed.collisions.geometry;
import org.lwjgl.util.Rectangle;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;


public class Edge {
	Point a;
	Point b;
	
	public Edge(Point a, Point b){
		this.a=a;
		this.b=b;
	}
	
	public boolean intersects(Edge other){
		
		
		return false;
	}
	
	public static boolean bound_intersect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4){
		Matrix4f mtx = new Matrix4f();
		
		mtx.translate(new Vector3f(x1,y1,x2));
		mtx.rotate(x3, new Vector3f(y3,x4,y4));
		mtx.scale(new Vector3f(x1,x2,x3));
		
		Vector4f v1 = Matrix4f.transform(mtx, new Vector4f(x1,x2,y1,1),null);
		Vector4f v2 = Matrix4f.transform(mtx, new Vector4f(x2,x3,y2,1),null);
		Vector4f v3 = Matrix4f.transform(mtx, new Vector4f(x3,x1,y3,1),null);
		Vector4f v4 = Matrix4f.transform(mtx, new Vector4f(x1,x2,y4,1),null);
		Vector4f v5 = Matrix4f.transform(mtx, new Vector4f(x2,x3,y1,1),null);
		Vector4f v6 = Matrix4f.transform(mtx, new Vector4f(x3,x1,y2,1),null);
		Vector4f v7 = Matrix4f.transform(mtx, new Vector4f(x1,x2,y3,1),null);
		Vector4f v8 = Matrix4f.transform(mtx, new Vector4f(x2,x3,y4,1),null);
		
		Point r1=new Point();
		Point r2=new Point();
		float w1,w2,h1,h2;
		if(x1<x2){
			r1.x=x1;
			w1 = Math.abs(x2-x1);
		}else{
			r1.x=x2;
			w1 = Math.abs(x1-x2);
		}
		if(y1<y2){
			r1.y=y1;
			h1 = Math.abs(y2-y1);
		}else{
			r1.y=y2;
			h1 = Math.abs(y1-y2);
		}
		//
		if(x3<x4){
			r2.x=x3;
			w2 = Math.abs(x4-x3);
		}else{
			r2.x=x4;
			w2 = Math.abs(x3-x4);
		}
		if(y3<y4){
			r2.y=y3;
			h2 = Math.abs(y4-y3);
		}else{
			r2.y=y4;
			h2 = Math.abs(y3-y4);
		}
		
		//return (r1.x+w1<r2.x) && ()
		return false;
		
	}
	public static Point intersects(float p0_x, float p0_y, float p1_x, float p1_y, float p2_x, float p2_y, float p3_x, float p3_y)
		{
			
		    float s1_x, s1_y, s2_x, s2_y;
		    s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
		    s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;

		    Point i = null;
		    
		    float s, t;
		    s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
		    t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

		    if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
		    {
		    	i = new Point();
	            i.x = p0_x + (t * s1_x);
	            i.y = p0_y + (t * s1_y);
		        return i;
		    }

		    return i; // No collision
		}
	
	public static void main(String[] args) {
		System.out.println(Edge.intersects(0, 0, 1, 1, 0, 1, 1, 0));
		long t0 = System.currentTimeMillis();
		int iCount = 0;
		for(int c=0;c<2000000;c++){
			if(Edge.intersects(
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100),
					(int)(Math.random()*100)
			)!=null){iCount++;}
		}
		System.out.println("iCount:"+iCount+" en "+(System.currentTimeMillis()-t0));
	}
}
